<template>
  <div>
    <!-- 面包屑导航栏 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>维修信息</el-breadcrumb-item>
      <el-breadcrumb-item>可抢单</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="5">
          <el-select
            clearable
            @clear="getRepairingList"
            v-model="queryInfo.repairType"
            placeholder="请选择维修类型"
            style="width: 100%"
          >
            <el-option label="电类故障" value="电类故障"></el-option>
            <el-option label="水类故障" value="水类故障"></el-option>
            <el-option label="木工类故障" value="木工类故障"></el-option>
            <el-option label="电梯故障" value="电梯故障"></el-option>
            <el-option label="空调故障" value="空调故障"></el-option>
            <el-option label="洗衣机故障" value="洗衣机故障"></el-option>
            <el-option label="直饮水故障" value="直饮水故障"></el-option>
            <el-option label="热水故障" value="热水故障"></el-option>
            <el-option label="其他类故障" value="其他类故障"></el-option>
          </el-select>
        </el-col>

        <el-col :span="5">
          <el-input
            placeholder="请输入维修人id"
            v-model="queryInfo.repID"
            clearable
            @clear="getRepairingList"
          >
          </el-input>
        </el-col>

        <el-col :span="5">
          <el-input
            placeholder="请输入楼栋"
            v-model="queryInfo.building"
            clearable
            @clear="getRepairingList"
          >
          </el-input>
        </el-col>

        <el-col :span="3">
          <el-button type="primary" @click="getRepairingList()"
            >开始查询</el-button
          >
        </el-col>

        <el-col :span="3">
          <el-button
            style="margin-left: -20px"
            type="primary"
            @click="showMyRepairman()"
            >我的任务</el-button
          >
        </el-col>
      </el-row>

      <!-- 表格数据区域 -->
      <el-table :data="RepairingList" stripe border>
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="userNumber" label="报修人"> </el-table-column>
        <el-table-column width="100px" prop="building" label="楼栋">
        </el-table-column>
        <el-table-column width="100px" prop="number" label="宿舍号">
        </el-table-column>
        <el-table-column prop="repairType" label="维修类型"> </el-table-column>
        <el-table-column sortable prop="reportTime" label="报修时间">
        </el-table-column>
        <el-table-column prop="repID" label="维修员">
          <template slot-scope="scope">
            <el-link
              @click="showRepairmanDialog(scope.row.repID)"
              type="primary"
              >{{ scope.row.repID }}</el-link
            >
          </template>
        </el-table-column>
        <el-table-column width="200px" prop="address" label="操作">
          <template slot-scope="scope">
            <!-- 编辑用户 -->
            <el-tooltip
              effect="dark"
              content="查看详情"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="primary"
                icon="el-icon-chat-line-square"
                :disabled="scope.row.repID != isRepId"
                size="mini"
                @click="showDetailDialog(scope.row)"
              ></el-button>
            </el-tooltip>

            <el-tooltip
              effect="dark"
              content="完成"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="success"
                :disabled="scope.row.repID != isRepId"
                icon="el-icon-check"
                size="mini"
                @click="showFinishDialog(scope.row.mainId)"
              ></el-button>
            </el-tooltip>

            <el-tooltip
              effect="dark"
              content="异常处理"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="danger"
                :disabled="scope.row.repID != isRepId"
                icon="el-icon-warning-outline"
                size="mini"
                @click="showErrorDialog(scope.row.mainId)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[2, 3, 6, 10]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 展示详细信息的对话框 -->
    <el-dialog
      title="详细信息"
      :visible.sync="showDetailDialogVisible"
      width="50%"
      @close="showDetailDalogClose"
    >
      <div class="demo-image__placeholder">
        <div class="block">
          <span class="demonstration" style="font-family: 100px Extra large"
            ><h2>报修描述：{{ detail.description }}</h2></span
          >
          <el-image :src="detail.imageUrl">
            <div slot="placeholder" class="image-slot">
              加载中<span class="dot">...</span>
            </div>
          </el-image>
        </div>
      </div>
    </el-dialog>

    <!-- 完成信息填写对话框 -->
    <el-dialog
      title="完成信息"
      :visible.sync="finishDialogVisible"
      width="50%"
      @close="finishDialogClose"
    >
      <!-- 主体 -->
      <el-form
        :hide-required-asterisk="true"
        ref="finishFormRef"
        :rules="finishFormRules"
        :model="finishForm"
        label-width="70px"
      >
        <el-form-item prop="repairDesc" label="维修结果">
          <el-input
            maxlength="100"
            show-word-limit
            type="textarea"
            v-model="finishForm.repairDesc"
          ></el-input>
        </el-form-item>

        <el-form-item label="图片描述">
          <!-- :on-remove="handleRemove" -->
          <el-upload
            class="upload-demo"
            action="http://localhost:8088/uploadPicture"
            :on-success="handleSuccess"
            list-type="picture"
            :limit="1"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">
              只能上传jpg/png文件，且不超过500kb,只能上传一张
            </div>
          </el-upload>
        </el-form-item>
      </el-form>
      <!-- 按钮部分 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="finishDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="finish">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 异常信息填写对话框 -->
    <el-dialog
      title="异常信息"
      :visible.sync="errorDialogVisible"
      width="50%"
      @close="errorDialogClose"
    >
      <!-- 主体 -->
      <el-form
        :hide-required-asterisk="true"
        ref="errorFormRef"
        :rules="errorFormRules"
        :model="errorForm"
        label-width="70px"
      >
        <el-form-item prop="errorReason" label="原因">
          <el-input
            maxlength="50"
            show-word-limit
            type="textarea"
            v-model="errorForm.errorReason"
          ></el-input>
        </el-form-item>
        <el-form-item prop="errorSuggest" label="建议">
          <el-input
            maxlength="50"
            show-word-limit
            type="textarea"
            v-model="errorForm.errorSuggest"
          ></el-input>
        </el-form-item>
        <el-form-item prop="errorDetail" label="详情">
          <el-input
            maxlength="50"
            show-word-limit
            type="textarea"
            v-model="errorForm.errorDetail"
          ></el-input>
        </el-form-item>
      </el-form>
      <!-- 按钮部分 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="errorDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleError">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 展示维修员信息 -->
    <el-dialog
      title="维修员信息"
      :visible.sync="showRepairmanDialogVisible"
      width="50%"
    >
      <el-descriptions class="margin-top" :column="3" border>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-user"></i>
            用户名
          </template>
          {{ repairman.repairmanName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-mobile-phone"></i>
            手机号
          </template>
          {{ repairman.repairmanTel }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-location-outline"></i>
            编号
          </template>
          {{ repairman.repairmanId }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-tickets"></i>
            维修类型
          </template>
          <el-tag size="small">{{ repairman.repairmanRepairtype }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-office-building"></i>
            联系地址
          </template>
          {{ repairman.repairmanAddress }}
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: { pageSize: 6 },
      RepairingList: [],
      total: 0,
      showDetailDialogVisible: false,
      detail: { imageUrl: "", description: "" },
      finishForm: {},
      finishDialogVisible: false,
      finishFormRules: {
        repairDesc: [
          { required: true, message: "请输入维修结果", trigger: "blur" },
        ],
      },
      isRepId: 0,
      errorDialogVisible: false,
      errorForm: {},
      errorFormRules: {
        errorReason: [
          { required: true, message: "请输入原因", trigger: "blur" },
        ],
        errorSuggest: [
          { required: true, message: "请输入建议", trigger: "blur" },
        ],
        errorDetail: [
          { required: true, message: "请输入详情", trigger: "blur" },
        ],
      },
      showRepairmanDialogVisible: false,
      repairman: {},
    };
  },
  methods: {
    //获取表格信息
    async getRepairingList() {
      const { data: res } = await this.$http.get("repairbill/repairing/", {
        params: this.queryInfo,
      });
      if (res.meta.status !== 200) {
        return this.$message.error("请求用户数据失败");
      }
      this.RepairingList = res.data.records;
      this.total = res.data.total;
    },

    // 页大小的变化
    handleSizeChange(newsize) {
      this.queryInfo.pageSize = newsize;
      this.getRepairingList();
    },
    // 页面值的变化
    handleCurrentChange(newPagenum) {
      this.queryInfo.pageNum = newPagenum;
      this.getRepairingList();
    },
    showDetailDialog(row) {
      this.showDetailDialogVisible = true;
      this.detail.imageUrl = row.reportImage;
      this.detail.description = row.reportDesc;
    },
    showDetailDalogClose() {
      this.detail.imageUrl = "";
      this.detail.description = "";
    },
    // 完成维修
    async finish() {
      const { data: res } = await this.$http.put(
        "repairbill/updaterepairing/",
        {
          mainId: this.finishForm.mainId,
          repairImage: this.finishForm.repairImage,
          repairDesc: this.finishForm.repairDesc,
        }
      );
      if (res.meta.status !== 200)
        return this.$message.error("操作失败，请联系有关人员");
      this.$message.success("操作成功");
      this.finishForm = {};
      this.finishDialogVisible = false;
      this.getRepairingList();
    },

    //展示完成模拟框
    showFinishDialog(id) {
      this.finishForm.mainId = id;
      this.finishDialogVisible = true;
    },
    //图片上传成功回调方法
    handleSuccess(rep) {
      alert(rep);
      this.$message.success("上传成功");
      this.finishForm.repairImage = rep;
    },
    //关闭前清空
    finishDialogClose() {
      this.$refs.finishFormRef.resetFields();
    },

    showErrorDialog(id) {
      this.errorDialogVisible = true;
      this.errorForm.mainId = id;
    },

    errorDialogClose() {
      this.$refs.errorFormRef.resetFields();
    },
    async handleError() {
      const { data: res } = await this.$http.put("repairbill/handleError/", {
        mainId: this.errorForm.mainId,
        errorReason: this.errorForm.errorReason,
        errorSuggest: this.errorForm.errorSuggest,
        errorDetail: this.errorForm.errorDetail,
      });
      if (res.meta.status !== 200)
        return this.$message.error("操作失败，请联系有关人员");
      this.$message.success("操作成功");
      this.errorForm = {};
      this.errorDialogVisible = false;
      this.getRepairingList();
    },

    async showRepairmanDialog(id) {
      this.showRepairmanDialogVisible = true;

      const { data: res } = await this.$http.get("getRepairmanById/" + id);
      if (res.meta.status !== 200)
        return this.$message.error("维修员信息获取失败，请联系有关人员");
      this.repairman = res.data;
    },

    showMyRepairman() {
      const user = JSON.parse(sessionStorage.getItem("user"));
      if (user.identity !== "维修员") {
        return this.$message.error("你没有该权限");
      }
      this.queryInfo.repID = user.username;
      this.getRepairingList();
    },
  },
  created() {
    this.getRepairingList();
    // this.flag =JSON.parse(sessionStorage.getItem("user")).identity !== "维修员";
    this.isRepId = JSON.parse(sessionStorage.getItem("user")).username;
    // console.log('flag'+this.flag);
    console.log(this.isRepId);
  },
};
</script>

<style></style>
